<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="98px">
      <el-form-item label="销售类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="销售类型" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.sale_type"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品品牌" prop="goodsBrand">
        <el-input v-model="queryParams.goodsBrand" placeholder="请输入产品品牌"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="销售日期" prop="type">
        <el-date-picker
          @change="selectDateRange"
          v-model="queryParams.dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否开票" prop="invoice">
        <el-select v-model="queryParams.invoice" placeholder="是否开票" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.yes_no"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
        <!--        <el-input v-model="queryParams.invoice" placeholder="请输入是否开票" clearable @keyup.enter.native="handleQuery"/>-->
      </el-form-item>
      <el-form-item label="是否回款" prop="fundCallback">
        <el-select v-model="queryParams.fundCallback" placeholder="是否回款" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.fund_callback"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
        <!--        <el-input v-model="queryParams.fundCallback" placeholder="请输入是否回款" clearable @keyup.enter.native="handleQuery"/>-->
      </el-form-item>
      <el-form-item label="销售人姓名" prop="saleName">
        <el-input v-model="queryParams.saleName" placeholder="请输入销售人姓名"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" >导出
        </el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini"  @click="handleBatch({})" >开具发票</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table" :max-height="tableMaxHeight" @sort-change="handleSortChange">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="客户名称" align="center" prop="customerName" width="160">
        <template slot-scope="scope">
          {{scope.row.customerName}}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectId">
        <template slot-scope="scope">
          <dict-tag :options="PrjNameOptions" :value="scope.row.projectId"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column sortable label="产品名称" align="center" prop="goodsName"  show-overflow-tooltip/>
      <el-table-column sortable label="产品品牌" align="center" prop="goodsBrand"/>
      <el-table-column sortable label="产品型号" align="center" prop="model"/>
      <el-table-column sortable label="单价" align="center" prop="goodsUnitPrice"/>
      <el-table-column sortable label="销售数量" align="center" prop="goodsCount"/>
      <el-table-column label="总价" align="center" prop="goodsTotalPrice"/>
      <el-table-column sortable label="销售人姓名" align="center" prop="saleName"/>
      <el-table-column sortable label="销售日期" align="center" prop="saleDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="是否提交开票" align="center" prop="invoice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.invoice"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="发票编号" align="center" prop="invoiceNo"/>-->
      <!--<el-table-column label="是否打印签收单" align="center" prop="receipt"/>-->
      <el-table-column sortable label="是否回款" align="center" prop="fundCallback">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fund_callback" :value="scope.row.fundCallback"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="销售类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_type" :value="scope.row.type"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认删除 `+scope.row.goodsName+` 数据项？`" @confirm="handleDelete(scope.row)"
                         style="margin-left: 10px;">
            <el-button v-if="scope.row.type==1" slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:GoodsSale:remove']">删除
            </el-button>
          </el-popconfirm>
          <span v-if="scope.row.type==2 || scope.row.type==3">
            请在项目中删除
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <goodsSale-form ref="form" @ok="getList"></goodsSale-form>
    <goods-invoice-batch ref="goodsInvoiceBatch" @ok="getList"></goods-invoice-batch>
  </div>
</template>

<script>
import { deleteAction, downloadFile, getAction, getById } from '@/api/manage'
    import GoodsSaleForm from '@/views/develop/goodsSale/module/GoodsSaleForm'
    import GoodsInvoiceBatch from '@/views/develop/goodsInvoice/module/GoodsInvoiceBatch'

    export default {
        name: "GoodsSaleList",
        dicts: ['fund_callback', 'sale_type', 'yes_no'],
        components: {
            GoodsSaleForm,
            GoodsInvoiceBatch
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 销售信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    goodsId: null,
                    goodsName: null,
                    goodsBrand: null,
                    goodsUnitPrice: null,
                    goodsCount: null,
                    goodsTotalPrice: null,
                    saleDate: null,
                    supplierId: null,
                    supplierName: null,
                    customerId: null,
                    customerName: null,
                    invoice: null,
                    invoiceId: null,
                    invoiceNo: null,
                    fundCallback: null,
                    receipt: null,
                    saleId: null,
                    saleName: null,
                    dateRange:"",
                    params:{},
                },
                url: {
                    listIds: '/goodsSale/goodsSale/listIds/',
                    list: '/goodsSale/goodsSale/list',
                    remove: '/goodsSale/goodsSale/',
                    export: '/goodsSale/goodsSale/export',
                    delete: '/goodsSale/goodsSale/deleteAndModidyOrderin/',
                    prjNames: '/projectBaseInfo/projectBaseInfo/prjNames',
                },
                PrjNameOptions:[],
                tableMaxHeight: window.innerHeight - 320,
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            };
        },
        created() {
            this.getList();
            this.intDictsForPrj();
            window.addEventListener('resize', this.getTableHeight);
        },
        destroyed () {
            window.removeEventListener('resize', this.getTableHeight)
        },
        methods: {

            selectDateRange(dateRange){
                let that = this;
                that.$nextTick(() => {
                    if (that.queryParams.dateRange != null && that.queryParams.dateRange.length > 0) {
                        that.dateRange = null;
                        that.$set(that.queryParams, "dateRange", [dateRange[0], dateRange[1]]);
                        that.queryParams.params.occurStartDate = dateRange[0];
                        that.queryParams.params.occurEndDate = dateRange[1];
                    } else {
                        that.queryParams.params.occurStartDate = null;
                        that.queryParams.params.occurEndDate = null;
                    }
                });
            },
            getTableHeight () {
                this.tableHeight = window.innerHeight - 320
            },
            intDictsForPrj(){
                let that = this;
                getAction( that.url.prjNames, {} ).then(res => {
                    that.PrjNameOptions = res.data.nameList;
                })
            },
            handleBatch(row) {
                const ids = row.id || this.ids;
                if (ids == null || ids == '' || ids == 'undefined') {
                    alert("请选择一条记录");
                } else {
                    getById(this.url.listIds, ids).then(response => {
                        if (response.rows.length > 0) {
                            this.$refs.goodsInvoiceBatch.show(ids);
                            /*this.columns = response.rows;
                            this.total = response.total;
                            this.loading = false;*/
                        } else {
                            alert("您选择多个客户的销售数据或销售数据中部分已经开具发票，请重新选择！");
                        }
                    });
                    //this.$refs.goodsInvoiceBatch.show(ids);
                }
            },
            /** 查询销售信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
             /** 排序触发事件 */
            handleSortChange(column, prop, order) {
              this.queryParams.orderByColumn = column.prop;
              this.queryParams.isAsc = column.order;
              this.handleQuery();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams.dateRange = null;
                this.queryParams.params.occurStartDate = null;
                this.queryParams.params.occurEndDate = null;
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.delete, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
              let that = this;
              that.loading = false;
              that.$confirm('是否确认导出销售信息表?', "确认", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }).then(function () {
                downloadFile('/goodsSale/goodsSale/exportSaleAll', that.queryParams);
              });
            },
        }
    };
</script>
